<?php

class PressCalcrow extends PressDirModel
{
		
	public $groupName='calcrow.group.ut_group_name';
	
	public function getShowType()
	{
		if (isset($_GET['show']))
			return ($_GET['show']);
		else
			return 'calcrows';
	}
	
	public function getViewColumns()
	{
		$result=array();
		$result[]='calcrow.ut_calcrow_name';
		switch ($this->showType)
		{
			case 'calcrows':
				$result[]= array(
		 			'name'=>'ut_calc_formula',
					'value'=>'CHtml::encode($data->ut_calc_formula)',
				);
				break;
			case 'resource':
				$result[]='ut_resource';
				$result[]='ut_price';
				break;
			case 'grade':
				$result[]='grade.ut_grade_name';
				$result[]='ut_time_norma';
				break;
		}
		return $result;
	}
	
	public function getUpdateFields()
	{
		$result=array();
		$result[]=array(
			'name'=>'ut_press_id',
			'editor'=>'ajaxDropDown',
			'data'=>CActiveRecord::model('Press')->listData(),
			'replace'=>'#'.CHtml::activeId($this,'ut_calcrow_id'),
		);
		switch ($this->showType)
		{
			case 'calcrows':
				$result[]=array(
					'name'=>'ut_calcrow_id',
					'editor'=>'dropDown',
					'data'=>(Yii::app()->controller->action->id=='create')?
							CActiveRecord::model('Calcrow')->absence(array('press'=>$this->ut_press_id))->listData('ut_calcrow_id','ut_calcrow_name', 'group.ut_group_name'):
							CActiveRecord::model('Calcrow')->select(array('calcrow_id'=>$this->ut_calcrow_id))->listData('ut_calcrow_id','ut_calcrow_name', 'group.ut_group_name'),
				);
				$result[]=array(
					'name'=>'ut_calc_formula',
					'editor'=>'memo',
					'cols'=>50,
					'rows'=>10,
				);
				break;
			case 'resource':
				$result[]=array(
					'name'=>'ut_calcrow_id',
					'editor'=>'dropDown',
				//	'label'=>CHtml::value($this,'calcrow.ut_calcrow_name'),
					'data'=>CActiveRecord::model('Calcrow')->select(array(':calcrow_id'=>$this->ut_calcrow_id))->listData('ut_calcrow_id','ut_calcrow_name', 'group.ut_group_name'),
				);
				$result[]='ut_resource';
				$result[]='ut_price';
				break;
			case 'grade':
				$result[]=array(
					'name'=>'ut_calcrow_id',
					'editor'=>'dropDown',
					'data'=>CActiveRecord::model('Calcrow')->select(array(':calcrow_id'=>$this->ut_calcrow_id))->listData('ut_calcrow_id','ut_calcrow_name', 'group.ut_group_name'),
				);
				$result[]=array(
					'name'=>'ut_grade_id',
					'editor'=>'dropDown',
					'data'=>CActiveRecord::model('Grade')->listData(),
				);
				$result[]='ut_time_norma';
				break;
		}  
		return $result;
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ut_press_calcrow';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ut_press_id, ut_calcrow_id', 'required'),
//			array('ut_calcrow_name', 'length', 'max'=>255),
			array('ut_press_id, ut_calcrow_id, ut_calc_formula', 'safe'),
			array('ut_calcrow_id, ut_press_id, ut_grade_id', 'type', 'type'=>'integer'),
			array('ut_price, ut_time_norma, ut_resource', 'type', 'type'=>'float'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'calcrow' => array(self::BELONGS_TO, 'Calcrow', 'ut_calcrow_id'),
			'press' => array(self::BELONGS_TO, 'Press', 'ut_press_id'),
			'grade' => array(self::BELONGS_TO, 'Grade', 'ut_grade_id'),	
			'calc' => array(self::HAS_MANY, 'UtCalc', 'ut_calcrow_id, ut_press_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ut_calcrow_id' => 'Расчетная строка',
			'ut_press_id'=>'Печатная машина',
			'ut_calc_formula' => 'Формула',
			'ut_resource' => 'Ресурс',
			'ut_price' => 'Стоимость',
			'ut_grade_id' => 'Разряд труда',
			'ut_time_norma' => 'Норма времени',
		);
	}

	public function search()
	{
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        $criteria->with='calcrow';
       	$criteria->compare('ut_press_id',empty($this->ut_press_id)?0:$this->ut_press_id);
			switch ($this->showType)
			{
				case 'resource':
					$criteria->addCondition('calcrow.ut_group_id=1');
					break;
				case 'grade':
					$criteria->addCondition('calcrow.ut_group_id=2');
					break;
			}
    //    $criteria->order='ut_calc_order';
      //  $criteria->compare('ut_base_format_id',$this->ut_base_format_id);
      //  $criteria->compare('ut_format_id',$this->ut_format_id);
      //  $criteria->compare('ut_exp_edition',$this->ut_exp_edition);
      //  $criteria->compare('ut_exp_count',$this->ut_exp_count);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
         		'pagination'=>false,
        ));
    }
	
	
}